package com.example.a;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity  {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button a = (Button)findViewById(R.id.button1);
        a.setBackgroundResource(R.drawable.f);
        Button b = (Button)findViewById(R.id.button2);
        Button c = (Button)findViewById(R.id.button3);
        Button d = (Button)findViewById(R.id.button4);
        final SeekBar p1 = (SeekBar)findViewById(R.id.processbar1);
        final SeekBar p2 = (SeekBar)findViewById(R.id.processbar2);
        final SeekBar p3 = (SeekBar)findViewById(R.id.processbar3);
        p1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				final SeekBar t = arg0;
				new Thread(new Runnable(){
					public void run(){
						try {
							DatagramSocket ds = new DatagramSocket();
							byte[] buf = ("hza\r\n").getBytes();
							buf[2] = (byte)(t.getProgress());
							DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
							ds.send(dp);
							ds.close();
						} catch (IOException e) {}
					}
				}).start();
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				final SeekBar t = arg0;
				new Thread(new Runnable(){
					public void run(){
						try {
							DatagramSocket ds = new DatagramSocket();
							byte[] buf = ("hza\r\n").getBytes();
							buf[2] = (byte)(t.getProgress());
							DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
							ds.send(dp);
							ds.close();
						} catch (IOException e) {}
					}
				}).start();
				
			}
		});

        p2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				final SeekBar t = arg0;
				new Thread(new Runnable(){
					public void run(){
						try {
							DatagramSocket ds = new DatagramSocket();
							byte[] buf = ("hya\r\n").getBytes();
							buf[2] = (byte)(t.getProgress());
							DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
							ds.send(dp);
							ds.close();
						} catch (IOException e) {}
					}
				}).start();
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				final SeekBar t = arg0;
				new Thread(new Runnable(){
					public void run(){
						try {
							DatagramSocket ds = new DatagramSocket();
							byte[] buf = ("hya\r\n").getBytes();
							buf[2] = (byte)(t.getProgress());
							DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
							ds.send(dp);
							ds.close();
						} catch (IOException e) {}
					}
				}).start();
				
			}
		});
        p3.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
			
			@Override
			public void onStopTrackingTouch(SeekBar arg0) {
				final SeekBar t = arg0;
				new Thread(new Runnable(){
					public void run(){
						try {
							DatagramSocket ds = new DatagramSocket();
							byte[] buf = ("hxa\r\n").getBytes();
							buf[2] = (byte)(t.getProgress());
							DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
							ds.send(dp);
							ds.close();
						} catch (IOException e) {}
					}
				}).start();
				
			}
			
			@Override
			public void onStartTrackingTouch(SeekBar arg0) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
				final SeekBar t = arg0;
				new Thread(new Runnable(){
					public void run(){
						try {
							DatagramSocket ds = new DatagramSocket();
							byte[] buf = ("hxa\r\n").getBytes();
							buf[2] = (byte)(t.getProgress());
							DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
							ds.send(dp);
							ds.close();
						} catch (IOException e) {}
					}
				}).start();
				
			}
		});
        a.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
					new Thread(new Runnable(){
						public void run(){
							try {
								DatagramSocket ds = new DatagramSocket();
								byte[] buf = "hz\063\r\n".getBytes();
								DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
								ds.send(dp);
								ds.close();
							} catch (IOException e) {}
						}
					}).start();
			}
		});
        b.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
					new Thread(new Runnable(){
						public void run(){
							try {
								DatagramSocket ds = new DatagramSocket();
								byte[] buf = "hb\r\n".getBytes();
								DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
								ds.send(dp);
								ds.close();
							} catch (IOException e) {}
						}
					}).start();
			}
		});
        c.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
					new Thread(new Runnable(){
						public void run(){
							try {
								DatagramSocket ds = new DatagramSocket();
								byte[] buf = "hc\r\n".getBytes();
								DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
								ds.send(dp);
								ds.close();
							} catch (IOException e) {}
						}
					}).start();
			}
		});
        d.setOnClickListener(new View.OnClickListener() {
		
		@Override
		public void onClick(View arg0) {
				new Thread(new Runnable(){
					public void run(){
						try {
							DatagramSocket ds = new DatagramSocket();
							byte[] buf = "hd\r\n".getBytes();
							DatagramPacket dp = new DatagramPacket(buf, buf.length, InetAddress.getByName("192.168.4.1"), 5000);
							ds.send(dp);
							ds.close();
						} catch (IOException e) {}
					}
				}).start();
		}
	});
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
